import {createSlice,createAsyncThunk} from "@reduxjs/toolkit"
import axios from "axios"

export let getroutes = createAsyncThunk("mygetroutes",async(id)=>{
    
   let res =  await axios.post("http://192.168.137.1:8001/getroutes",{id}).then((res)=>{
    localStorage.setItem("userroute",JSON.stringify(res.data.data))
    return res.data.data
   })
//    console.log("action",res)
   return res
})


let _loacaluserRoute = localStorage.getItem("userroute")
let userSlice =  createSlice({
    name:"userinfo",
    initialState:{
        userRoute:_loacaluserRoute ? JSON.parse(_loacaluserRoute) : []
    },
    reducers:{
        changeRoute(state,action){
            state.userRoute = action.payload
        }
    },
    extraReducers:(chunk)=>{
        chunk.addCase(getroutes.fulfilled,(start,action)=>{
           
            start.userRoute = action.payload
        })
    }
})

export let {changeRoute}  = userSlice.actions

export default userSlice.reducer